<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
define("TABLE", "keyvaluebydate");
//删除
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'del'){
		$db->sql = "DELETE FROM `".TABLE."` WHERE id = ".$_GET['id'];
		$db->doExecute();
	}
	elseif ($act == "piliangAdd"){
		$comments = isset($_POST["comments"])?$_POST["comments"]:'';
		if (!empty($comments)) {
			$commentsStr = str_replace(array("\r\n", "\n", "\r"), '###', ($comments));
			$comment_arry = explode("###", $commentsStr);
			$comment_arry2 = array();
			foreach ($comment_arry as $item){
				$question_answers=explode("||", $item);
				$db->sql = "INSERT INTO `".TABLE."`(`question`,`answer`,`createtime`) VALUES('{$question_answers[0]}','{$question_answers[1]}',".time().")";
				$db->doExecute();
			}
			//var_dump($comment_arry2);exit();
		}
	}
}
//keyword
if(isset($_GET['name'])){
	if (!empty($_GET['name'])) {
		$name = $_GET['name'];
		$where = " AND `name` like '%{$name}%' ";
	}else{
		$keyword='';
		$where = '';
	}
}else{
	$keyword='';
	$where = '';
}
//总条数
$db->sql="SELECT COUNT(`id`) FROM `".TABLE."`  WHERE 1=1 {$where}";
$count = $db->doResultOne();
//分页
if(empty($_GET['page'])){$page = 1;}else{$page=$_GET['page'];}
if(empty($_GET['pagesize'])){$pagesize = 50;}
else{$pagesize = $_GET['pagesize'];}
$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];
//var_dump($pageInfo);
$db->sql="SELECT * FROM `".TABLE."` WHERE 1=1 {$where} ORDER BY id DESC LIMIT {$start},{$pagesize}";
$comments = $db->doResultList2();

$url="?page=".$page."&name=".$name;
//var_dump($comments);
//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;}
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	$(".deleteUser").click(function(){
		if(!confirm("是否将此兑换问题删除?")){
			return false;
		}
	});
	//批量删除
	$(".piliangshanchu").click(function(){
		if(confirm("是否删除这些商品！")){
			var ids='';
			if(!$("input[name=picSel]:checked").size()){
				alert("没有要删除的数据！");	
				return false;
			} 
			$("input[name=picSel]:checked").each(function() {
				ids+=$(this).val()+',';
				$(this).parents("tr").remove();
			});
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "ids="+ids+"&act=piliangshanchu2byids&table=<?php echo TABLE;?>&",
				dataType: "html",
				success: function(date){
					alert("删除成功");
					
				}
			}); 
			return false;
		}
	});
});
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>问题批量添加</strong>
			</p>
			<form action="?act=piliangAdd" method="post">
				<fieldset>
					<!-- begin tip -->
					<div class="notification information png_bg">
						<a href="javascript:;" class="close" onclick="$(this).parent('div').hide();"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
						<div>
							注意添加时，用换行批量添加多个问题！<br />问题与答案之间用||隔开。如：<br /><br />你好吗？||不好<br />李雷57岁，他儿子小他27岁，李雷几岁？||57<br />...<br />...<br />...
						</div>
					</div>
					<!-- end tip -->
					<p>批量添加问题：</p>
					<textarea class="textarea" id="comments" name="comments" style="width: 380px;height: 90px;"></textarea>
					<input class="button" type="submit" value="批量添加" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide().siblings('#facebox_overlay').remove();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">兑换问题管理</p>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>兑换问题管理</h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						修改成功
					</div>
				</div>
				
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
						
						<table>
							<thead>
								<tr>
									<td colspan="9">
										<div class="bulk-actions align-left" style="border: 1px solid #E7E7E7;padding: 5px;">
										<form action="#" method="get" style="width: 500px">
											统计类:<input type="text" class="text-input" value="<?php echo $name;?>" name="name" />
											<button class="button sousuo" type="submit">搜索</button>
										</form>
										</div>
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1&keyword=<?php echo $keyword;?>" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>&keyword=<?php echo $keyword;?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>&keyword=<?php echo $keyword;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>&keyword=<?php echo $keyword;?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>&keyword=<?php echo $keyword;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
										<div class="clear"></div>
										
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th>ID</th>
									<th style="">统计类</th>
									<th style="">日期</th>
									<th style="">数量</th>
								</tr>

							</thead>

							<tbody>
							<?php if($count>0):?>
							<?php foreach ($comments as $key=>$item):?>
								<tr>
									<td><?php echo $item['id'];?></td>
									<td><?php echo $item['name'];?></td>
									<td><?php echo $item['date'];?></td>
									<th><?php echo $item['num'];?></th>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">没有兑换问题！！</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="9">
									<div class="clear"></div>
										
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1&keyword=<?php echo $keyword;?>" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>&keyword=<?php echo $keyword;?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>&keyword=<?php echo $keyword;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>&keyword=<?php echo $keyword;?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>&keyword=<?php echo $keyword;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
									</td>
								</tr>
							</tfoot>
						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
